package com.ltcode.sys.interfaces.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ltcode.common.base.web.domain.ApiResponse;
import com.ltcode.sys.application.service.SysUserAppService;
import com.ltcode.sys.domain.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息控制器
 * 
 * @author ltcode
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserAppService userService;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public ApiResponse<List<SysUser>> list(SysUser user) {
        List<SysUser> list = userService.selectUserList(user);
        return ApiResponse.ok(list);
    }


    /**
     * 修改用户
     */
    @PutMapping
    public ApiResponse<Void> edit(@Validated @RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getId());
        if (!userService.checkUserNameUnique(user)) {
            return ApiResponse.fail("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (!userService.checkPhoneUnique(user)) {
            return ApiResponse.fail("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (!userService.checkEmailUnique(user)) {
            return ApiResponse.fail("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        userService.updateUser(user);
        return ApiResponse.ok();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{userIds}")
    public ApiResponse<Void> remove( @PathVariable Long[] userIds) {
        if (userIds != null && userIds.length > 0) {
            for (Long userId : userIds) {
                SysUser user = new SysUser();
                user.setId(userId);
                userService.checkUserAllowed(user);
                userService.checkUserDataScope(userId);
            }
        }
        userService.deleteUserByIds(userIds);
        return ApiResponse.ok();
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd")
    public ApiResponse<Void> resetPwd(@RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getId());
        userService.resetPwd(user);
        return ApiResponse.ok();
    }

    /**
     * 状态修改
     */
    @PutMapping("/changeStatus")
    public ApiResponse<Void> changeStatus(@RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getId());
        userService.updateUserStatus(user);
        return ApiResponse.ok();
    }


    /**
     * 用户授权角色
     */
    @PutMapping("/authRole")
    public ApiResponse<Void> insertAuthRole(Long userId, Long[] roleIds) {
        userService.checkUserDataScope(userId);
        userService.insertUserAuth(userId, roleIds);
        return ApiResponse.ok();
    }

    /**
     * 获取部门树列表
     */
    @GetMapping("/deptTree")
    public ApiResponse<?> deptTree(SysUser user) {
        // TODO: 获取部门树信息
        return ApiResponse.ok();
    }
}
